/*
 * contrib/btree_gist/btree_text.c
 */
#include "postgres.h"

#include "btree_gist.h"
#include "btree_utils_var.h"
#include "utils/builtins.h"

/*
** 文本操作
*/
PG_FUNCTION_INFO_V1(gbt_text_compress);
PG_FUNCTION_INFO_V1(gbt_bpchar_compress);
PG_FUNCTION_INFO_V1(gbt_text_union);
PG_FUNCTION_INFO_V1(gbt_text_picksplit);
PG_FUNCTION_INFO_V1(gbt_text_consistent);
PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
PG_FUNCTION_INFO_V1(gbt_text_penalty);
PG_FUNCTION_INFO_V1(gbt_text_same);


/* 定义比较 */

static bool fc_gbt_textgt(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(text_gt,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_textge(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(text_ge,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_texteq(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(texteq,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_textle(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(text_le,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_textlt(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(text_lt,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static int32 fc_gbt_textcmp(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetInt32(DirectFunctionCall2Coll(bttextcmp,
												 fc_collation,
												 PointerGetDatum(fc_a),
												 PointerGetDatum(fc_b)));
}

static gbtree_vinfo tinfo =
{
	gbt_t_text,
	0,
	false,
	fc_gbt_textgt,
	fc_gbt_textge,
	fc_gbt_texteq,
	fc_gbt_textle,
	fc_gbt_textlt,
	fc_gbt_textcmp,
	NULL
};

/* bpchar 需要自己的比较规则 */

static bool fc_gbt_bpchargt(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(bpchargt,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_bpcharge(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(bpcharge,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_bpchareq(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(bpchareq,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_bpcharle(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(bpcharle,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static bool fc_gbt_bpcharlt(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetBool(DirectFunctionCall2Coll(bpcharlt,
												fc_collation,
												PointerGetDatum(fc_a),
												PointerGetDatum(fc_b)));
}

static int32 fc_gbt_bpcharcmp(const void *fc_a, const void *fc_b, Oid fc_collation, FmgrInfo *fc_flinfo)
{
	return DatumGetInt32(DirectFunctionCall2Coll(bpcharcmp,
												 fc_collation,
												 PointerGetDatum(fc_a),
												 PointerGetDatum(fc_b)));
}

static gbtree_vinfo bptinfo =
{
	gbt_t_bpchar,
	0,
	false,
	fc_gbt_bpchargt,
	fc_gbt_bpcharge,
	fc_gbt_bpchareq,
	fc_gbt_bpcharle,
	fc_gbt_bpcharlt,
	fc_gbt_bpcharcmp,
	NULL
};


/**************************************************
 * 文本操作
 **************************************************/


Datum gbt_text_compress(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);

	if (tinfo.eml == 0)
	{
		tinfo.eml = pg_database_encoding_max_length();
	}

	PG_RETURN_POINTER(gbt_var_compress(fc_entry, &tinfo));
}

Datum gbt_bpchar_compress(PG_FUNCTION_ARGS)
{
	/* 这不应该与 gbt_text_compress 有区别 */
	return gbt_text_compress(fcinfo);
}



Datum gbt_text_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	void	   *fc_query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	bool		fc_retval;
	GBT_VARKEY *fc_key = (GBT_VARKEY *) DatumGetPointer(fc_entry->key);
	GBT_VARKEY_R fc_r = gbt_var_key_readable(fc_key);

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	if (tinfo.eml == 0)
	{
		tinfo.eml = pg_database_encoding_max_length();
	}

	fc_retval = gbt_var_consistent(&fc_r, fc_query, fc_strategy, PG_GET_COLLATION(),
								GIST_LEAF(fc_entry), &tinfo, fcinfo->flinfo);

	PG_RETURN_BOOL(fc_retval);
}


Datum gbt_bpchar_consistent(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_entry = (GISTENTRY *) PG_GETARG_POINTER(0);
	void	   *fc_query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
	StrategyNumber fc_strategy = (StrategyNumber) PG_GETARG_UINT16(2);

	/* Oid		subtype = PG_GETARG_OID(3); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(4);
	bool		fc_retval;
	GBT_VARKEY *fc_key = (GBT_VARKEY *) DatumGetPointer(fc_entry->key);
	GBT_VARKEY_R fc_r = gbt_var_key_readable(fc_key);

	/* 由此函数服务的所有案例都是精确的 */
	*fc_recheck = false;

	if (bptinfo.eml == 0)
	{
		bptinfo.eml = pg_database_encoding_max_length();
	}

	fc_retval = gbt_var_consistent(&fc_r, fc_query, fc_strategy, PG_GET_COLLATION(),
								GIST_LEAF(fc_entry), &bptinfo, fcinfo->flinfo);
	PG_RETURN_BOOL(fc_retval);
}


Datum gbt_text_union(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	int32	   *fc_size = (int *) PG_GETARG_POINTER(1);

	PG_RETURN_POINTER(gbt_var_union(fc_entryvec, fc_size, PG_GET_COLLATION(),
									&tinfo, fcinfo->flinfo));
}


Datum gbt_text_picksplit(PG_FUNCTION_ARGS)
{
	GistEntryVector *fc_entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
	GIST_SPLITVEC *fc_v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);

	gbt_var_picksplit(fc_entryvec, fc_v, PG_GET_COLLATION(),
					  &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_v);
}

Datum gbt_text_same(PG_FUNCTION_ARGS)
{
	Datum		fc_d1 = PG_GETARG_DATUM(0);
	Datum		fc_d2 = PG_GETARG_DATUM(1);
	bool	   *fc_result = (bool *) PG_GETARG_POINTER(2);

	*fc_result = gbt_var_same(fc_d1, fc_d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
	PG_RETURN_POINTER(fc_result);
}


Datum gbt_text_penalty(PG_FUNCTION_ARGS)
{
	GISTENTRY  *fc_o = (GISTENTRY *) PG_GETARG_POINTER(0);
	GISTENTRY  *fc_n = (GISTENTRY *) PG_GETARG_POINTER(1);
	float	   *fc_result = (float *) PG_GETARG_POINTER(2);

	PG_RETURN_POINTER(gbt_var_penalty(fc_result, fc_o, fc_n, PG_GET_COLLATION(),
									  &tinfo, fcinfo->flinfo));
}
